package com.bwie.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @description:
 * @author: zyc
 * @create: 2024-07-22 08:52
 **/
@Configuration
public class GateWayFilter implements GlobalFilter, Ordered {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
    * @Description: 网关过滤器
    * @Param: [exchange, chain]
    * @return: reactor.core.publisher.Mono<java.lang.Void>
    * @Author: Zhang Yucong
    * @Date: 2024/7/22
    */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取参数
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //获取ip
        String ip = request.getRemoteAddress().getAddress().toString();
        //获取url
        String url = request.getPath().toString();
        String token = request.getHeaders().getFirst("token");

        String token1 = request.getHeaders().getFirst("token");
        System.out.println(token1);

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
